package com.resource.utils;


import lombok.extern.slf4j.Slf4j;

import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 执行结果包装器
 *
 * @author Tao.yu
 * @version 2019/11/5 14:49
 **/
@Slf4j
public class ServiceResultHandler {

	/**
	 * 接受一个参数无返回结果
	 *
	 * @param check
	 * @return
	 */
	public static ResultBody process(Consumer<Object> check, Object params) {
		try {
			check.accept(params);
			return ResultBody.builder().success().build();
		} catch (ServiceException e) {
			return ResultBody.builder().failed(e.getErrorCode(), e.getErrorMessage()).build();
		} catch (Exception e) {
			return ResultBody.builder().failed(ServiceException.unchecked(e)).build();
		}
	}

	/**
	 * 无参数返回一个结果
	 *
	 * @param check
	 * @return
	 */
	public static ResultBody process(Supplier<Object> check) {
		try {
			Object result = check.get();
			return ResultBody.builder().success(result).build();
		} catch (ServiceException e) {
			return ResultBody.builder().failed(e.getErrorCode(), e.getErrorMessage()).build();
		} catch (Exception e) {
			return ResultBody.builder().failed(ServiceException.unchecked(e)).build();
		}
	}

	/**
	 * 接受一个参数返回一个结果
	 *
	 * @param execute
	 * @param params
	 * @return
	 * @throws ServiceException
	 */
	public static ResultBody process(Function<Object, Object> execute, Object params) throws ServiceException {
		try {
			Object result = execute.apply(params);
			return ResultBody.builder().success(result).build();
		} catch (ServiceException e) {
			return ResultBody.builder().failed(e.getErrorCode(), e.getErrorMessage()).build();
		} catch (Exception e) {
			return ResultBody.builder().failed(ServiceException.unchecked(e)).build();
		}
	}

	/**
	 * 接受两个参数返回一个结果
	 *
	 * @param execute
	 * @param param1
	 * @param param2
	 * @return
	 * @throws ServiceException
	 */
	public static ResultBody process(BiFunction<Object, Object, Object> execute, Object param1, Object param2) throws ServiceException {
		try {
			Object result = execute.apply(param1, param2);
			return ResultBody.builder().success(result).build();
		} catch (ServiceException e) {
			return ResultBody.builder().failed(e.getErrorCode(), e.getErrorMessage()).build();
		} catch (Exception e) {
			return ResultBody.builder().failed(ServiceException.unchecked(e)).build();
		}
	}
	// 入参无需校验-三个参数,建议入参大于3个的service方法，将参数包装成一个vo对象

	/**
	 * 接受一个参数返回一个结果和成功后的message
	 *
	 * @param execute
	 * @param params
	 * @return
	 * @throws ServiceException
	 */
	public static ResultBody process(Function<Object, Object> execute, Object params, String message)
			throws ServiceException {
		try {
			Object result = execute.apply(params);
			return ResultBody.builder().success(message, result).build();
		} catch (ServiceException e) {
			return ResultBody.builder().failed(e.getErrorCode(), e.getErrorMessage()).build();
		} catch (Exception e) {
			return ResultBody.builder().failed(ServiceException.unchecked(e)).build();
		}
	}

	/**
	 * 接受一个参数无返回结果成功返回message信息
	 *
	 * @param check
	 * @return
	 */
	public static ResultBody process(Consumer<Object> check, Object params, String message) {
		try {
			check.accept(params);
			return ResultBody.builder().success().message(message).build();
		} catch (ServiceException e) {
			return ResultBody.builder().failed(e.getErrorCode(), e.getErrorMessage()).build();
		} catch (Exception e) {
			return ResultBody.builder().failed(ServiceException.unchecked(e)).build();
		}
	}
}
